/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: MainConfigOfProfile
 * Author:   zhouheng
 * Date:     2018/5/25 17:11
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zhou.annotation.config;

import com.mchange.v2.c3p0.jboss.C3P0PooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2018/5/25
 * @since 1.0.0
 */
@PropertySource(value = "dbconfig.properties")
@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware{
    public StringValueResolver stringValueResolver;

    @Value("${db.driverClass}")
    public String driverCass;

    @Bean
    @Profile("test")
    public C3P0PooledDataSource datasourceTest() throws Exception{
        C3P0PooledDataSource c3P0PooledDataSource = new C3P0PooledDataSource();
        c3P0PooledDataSource.setUser("${db.user}");
        c3P0PooledDataSource.setPassword("${db.password}");
        c3P0PooledDataSource.setDriverClass(driverCass);
        c3P0PooledDataSource.setJdbcUrl("jdbc:mysql:127.0.0.1:3306/test");

        return c3P0PooledDataSource;

    }

    @Bean
    @Profile("dev")
    public C3P0PooledDataSource datasourceDev(@Value("${db.user}")String user,@Value("${db.passWord}")String password) throws Exception{
        C3P0PooledDataSource c3P0PooledDataSource = new C3P0PooledDataSource();
        c3P0PooledDataSource.setUser(user);
        c3P0PooledDataSource.setPassword(password);
        c3P0PooledDataSource.setDriverClass(this.stringValueResolver.resolveStringValue("${db.driverClass}"));
        c3P0PooledDataSource.setJdbcUrl("jdbc:mysql:127.0.0.1:3306/dev");

        return c3P0PooledDataSource;
    }

    @Bean
    @Profile("proc")
    public C3P0PooledDataSource datasourceProd() throws Exception{
        C3P0PooledDataSource c3P0PooledDataSource = new C3P0PooledDataSource();
        c3P0PooledDataSource.setUser("${db.user}");
        c3P0PooledDataSource.setPassword("${db.password}");
        c3P0PooledDataSource.setDriverClass("com.mysql.jdbc.driver");
        c3P0PooledDataSource.setJdbcUrl("jdbc:mysql:127.0.0.1:3306/prod");

        return c3P0PooledDataSource;

    }


    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.stringValueResolver = resolver;
    }
}